﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using ImportDataUtil;

namespace ImportDataCore
{
    public partial class ImportMainFrom : DevExpress.XtraEditors.XtraForm
    {

        public enum EnumCurrentState
        {
            Running=1,
            Finished=2,
            Ready=3
        }

        UCCheck _ucCheck = null;
        UCInput _ucInput = null;
        UCLog _ucLog = null;



        CheckChain _checkChain = null;
        Import _import = null;
        IAnalyDataSource analyDataSource = null;
        LogCenter logs = null;

        EnumCurrentState _enumCheckCurrentState = EnumCurrentState.Ready;
        EnumCurrentState _enumInputCurrentState = EnumCurrentState.Ready;

        bool _bIsPreSuccess = true;

        string _dataPath = string.Empty;

        public ImportMainFrom()
        {
            InitializeComponent();

            Init();
        }

        private void wizImport_NextClick(object sender, DevExpress.XtraWizard.WizardCommandButtonClickEventArgs e)
        {
            _dataPath = this._ucCheck.FilePath;
            if (!CheckPara())
            {
                e.Handled = true;
                return;
            }

            if (this.wizImport.SelectedPage == pageCheck)
            {
                _bIsPreSuccess = Check();
                if (!_bIsPreSuccess)
                {
                    MessageBox.Show("质量检查出现错误，请查看");
                    e.Handled = true;
                    return;
                }
            }
            
        }

        private void wizImport_SelectedPageChanged(object sender, DevExpress.XtraWizard.WizardPageChangedEventArgs e)
        {
            if (e.Page == this.pageInput && _enumInputCurrentState!=EnumCurrentState.Finished)
            {
                ImportData();
            }
            if (e.Page == this.pageLog)
            {

                _ucLog.ClearLog();

                StringBuilder strBuilder = new StringBuilder();
                strBuilder.Append(logs.GetLog());
                _ucLog.AddLog(strBuilder);
            }
        }

        private bool CheckPara()
        {
            if (wizImport.SelectedPage==this.pageCheck)
            {
                if (_enumCheckCurrentState == EnumCurrentState.Running)
                {
                    return false;
                }
                if (string.IsNullOrEmpty(_dataPath) || !System.IO.File.Exists(_dataPath))
                {
                    MessageBox.Show("数据路径无效");
                    return false;
                }
            }

            if (wizImport.SelectedPage == this.pageInput)
            {
                if (_enumInputCurrentState == EnumCurrentState.Running)
                {
                    return false;
                }
            }

            return true;
        }

        private bool Check()
        {
            ClearBeforImport();

            bool isSuccess=false;
            _enumCheckCurrentState = EnumCurrentState.Running;

            try
            {
                analyDataSource.FilePath = _dataPath;
                analyDataSource.AnalyDataSource();

                if (analyDataSource.LstBaseSourceModel == null) return isSuccess;

                _checkChain.LstSourceModels = analyDataSource.LstBaseSourceModel;
                isSuccess=_checkChain.check();
            }
            catch (Exception ex)
            {
                LogHelper.ErrorLog.AppendErrorMsg(ex.Message);
            }
            finally
            {
                if (_enumCheckCurrentState == EnumCurrentState.Running)
                    _enumCheckCurrentState = EnumCurrentState.Finished;
            }

            return isSuccess;
        }

        private bool ImportData()
        {
            bool isSuccess = false;
            _enumInputCurrentState = EnumCurrentState.Running;

            try
            {
                _import.LstSourceModel = analyDataSource.LstBaseSourceModel;
                isSuccess=_import.Input();
            }
            catch (Exception ex)
            {
                LogHelper.ErrorLog.AppendErrorMsg(ex.Message);
            }
            finally
            {
                if (_enumInputCurrentState == EnumCurrentState.Running)
                    _enumInputCurrentState = EnumCurrentState.Finished;
            }

            return isSuccess;
        }

        private void InitCheckChain()
        {
            if (_checkChain == null)
                _checkChain = new CheckChain();

            ICheckRule rule = new GenerateCheck();
            rule.ProgressInitEvent += _ucCheck.InitProgress;
            rule.NotifyLogInfoEvent += _ucCheck.NotifyLogInfo;
            rule.NotifyProgressEvent += _ucCheck.NotifyProgress;
            _checkChain.AddRule(rule);

            //rule = new ReproducibilityCheck();
            //rule.ProgressInitEvent += _ucCheck.InitProgress;
            //rule.NotifyLogInfoEvent += _ucCheck.NotifyLogInfo;
            //rule.NotifyProgressEvent += _ucCheck.NotifyProgress;
            //_checkChain.AddRule(rule);
        }

        private void InitAnalySource()
        {
            analyDataSource = new ExcelAnaly();
            ((ExcelAnaly)analyDataSource).NotifyLogEvent += new NotifyLogInfoEventHandler(_ucCheck.NotifyLogInfo);
        }

        private void InitUI()
        {
            _ucCheck = new UCCheck();
            _ucCheck.Dock = DockStyle.Fill;
            _ucCheck.RegisterLogToLogCenter += new RegisterLogToLogCenterEventHandler(RegisterLogToLogcenter);
            _ucCheck.AfterSetPathEvent += new UCCheck.AfterSetPathEventHandler(AfterSetPath);
            this.pageCheck.Controls.Add(_ucCheck);

            _ucInput = new UCInput();
            _ucInput.Dock = DockStyle.Fill;
            _ucInput.RegisterLogToLogCenter += new RegisterLogToLogCenterEventHandler(RegisterLogToLogcenter);
            this.pageInput.Controls.Add(_ucInput);

            _ucLog = new UCLog();
            _ucLog.Dock = DockStyle.Fill;
            this.pageLog.Controls.Add(_ucLog);

        }

        private void Init()
        {
            InitUI();

            InitCheckChain();

            InitAnalySource();

            InitLog();

            InitImport();
        }

        private void InitLog()
        {
            logs = new LogCenter();
        }

        private void InitImport()
        {
            if (_import == null)
                _import = new Import();
            _import.NotifyLogInfoEvent += new NotifyLogInfoEventHandler(_ucInput.NotifyLogInfo);
            _import.NotifyProgressEvent += new NotifyProgreeEventHandler(_ucInput.NotifyProgress);
            _import.ProgressInitEvent += new  ProgressInitEventHandler(_ucInput.InitProgress);
        }

        private void RegisterLogToLogcenter(LogEventArgs agrs, EnumOperaterType operaterType)
        {
            if (agrs == null)
                return;
            LogInfo info = new LogInfo();
            info.SDataPath = this._dataPath;
            info.ICol = agrs.IColNum;
            info.IRow = agrs.IRowNum;
            info.SErrorMsg = agrs.LogMsg;
            info.EnumOperaterType = operaterType;

            if (logs == null)
            {
                logs = new LogCenter();
            }

            logs.AddLog(info);
        }

        private void AfterSetPath(string path)
        {
            _enumCheckCurrentState = EnumCurrentState.Ready;
            _enumInputCurrentState = EnumCurrentState.Ready;
        }

        private void ClearBeforImport()
        {
            //界面
            this._ucCheck.Clearlog();
            this._ucInput.ClearLog();
            this._ucLog.ClearLog();
            this.logs.ClearLog();
        }

        private void wizImport_PrevClick(object sender, DevExpress.XtraWizard.WizardCommandButtonClickEventArgs e)
        {
            if (_enumCheckCurrentState == EnumCurrentState.Running || _enumInputCurrentState == EnumCurrentState.Running)
            {
                e.Handled = true;
            }
        }

        private void wizImport_CancelClick(object sender, CancelEventArgs e)
        {
            if (_enumInputCurrentState == EnumCurrentState.Running || _enumInputCurrentState == EnumCurrentState.Running)
            {
                MessageBox.Show("程序正在计算，暂时不能退出");
                Application.DoEvents();
                return;
            }

            Application.Exit();
        }

        private void wizImport_FinishClick(object sender, CancelEventArgs e)
        {
            Application.Exit();
        }

        private void wizImport_HelpClick(object sender, DevExpress.XtraWizard.WizardButtonClickEventArgs e)
        {
            MessageBox.Show("呵呵，请联系我");
        }

    }
}